<template>
  <div class="add">
    <el-form ref="form" :model="form" label-width="8rem">
      <el-form-item label="部门编号">
        <el-input v-model="form.depCode"></el-input>
      </el-form-item>
      <el-form-item label="部门名称">
        <el-input v-model="form.depName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addData()">立即创建</el-button>
        <el-button type="default" @click="back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import $ from "@/api/departStaff";

  export default {
    data() {
      return {
        form: {
          depCode: "",
          depName: ""
        },

      };
    },
    methods: {
      addData() {
        if (!this.validate()) return;
        $.tbDmAddDm(this.form).then(response => {
          let messageType = ""
          if (response.data == "添加成功") messageType = "success"
          else messageType = "error"
          this.$message({
            type: messageType,
            message: response.data
          });
          this.$router.replace('index')
        });
      },
      validate() {
        let error = "";
        if (this.form.depCode == "" || this.form.depCode == null) {
          error = "编号不能为空\n";
        } else if (this.form.depName == "" || this.form.depName == null) {
          error = "部门名称不能为空\n"
        }
        if (error) {
          this.$message({
            message: error,
            type: "error"
          });
          return false;
        }
        return true;
      },
      back() {
        this.$router.replace('index')
      }
    }
  };
</script>
<style scoped>
  .add {
    padding: 1rem;
  }
</style>
